package com.grz.springbootdemo.config;

import com.grz.core.MyInterceptor;
import com.grz.core.MyInterceptor2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @author cyrus
 */
@Configuration
@ComponentScan("com.grz.controller")
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    @Autowired
    private MyInterceptor myInterceptor;
    @Autowired
    private MyInterceptor2 myInterceptor2;

    @Autowired
    private UploadProperty uploadProperty;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor)
                // 哪些接口需要增加拦截器
//                .addPathPatterns("/book/**")
                .addPathPatterns("/**")
                // 配置不需要走的接口
                .excludePathPatterns("/login","/register","/")
                .excludePathPatterns("/profile/**")
        ;

        registry.addInterceptor(myInterceptor2);
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置静态资源映射
        registry.addResourceHandler("/profile/**")
                // file:代表文件协议
                // classpath:从resource中找
                .addResourceLocations("file:"+uploadProperty.getLocalPath());
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        // 转化器的作用，就是告诉MVC不同格式的数据怎么转化
        registry.addConverter(new Converter<String, Date>() {
            @Override
            public Date convert(String s) {
                SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
                try {
                    return sdf.parse(s);
                } catch (ParseException e) {
                    e.printStackTrace();
                    throw new RuntimeException("日期解析异常");
                }
            }
        });

        //用来转换String至LocalDateTime类型
        registry.addConverter(new Converter<String, LocalDateTime>() {
            @Override
            public LocalDateTime convert(String source) {
                return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(DEFAULT_DATETIME_FORMAT));
            }
        });
        //用来转换String至LocalTime类型
        registry.addConverter(new Converter<String, LocalTime>() {
            @Override
            public LocalTime convert(String source) {
                return LocalTime.parse(source, DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT));
            }
        });
        registry.addConverter(new Converter<String, LocalDate>() {
            @Override
            public LocalDate convert(String source) {
                return LocalDate.parse(source, DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));
            }
        });
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(new JacksonObjectMapper());
        converters.add(converter);
    }
}
